// Copyright (c) 2021 Presto Labs Pte. Ltd.
// Author: jshin

#pragma once

#include <memory>
#include <string>
#include <thread>
#include <vector>

#include "coin2/strategy/config_service.pb.h"

namespace coin2::strategy::util {

class ConfigReporter_GrpcImpl;

class ConfigReporter {
 public:
  using Handler = std::function<void(const InternalControlRequest&)>;

 public:
  ConfigReporter(
      const std::string& strategy_name,
      const std::string& server_address,
      const std::vector<std::string>& symbols,
      Handler handler);

  ~ConfigReporter();

  void Start();
  void Shutdown();
  void SubmitInternalControlRequest(const InternalControlRequest ctrl_req);
  void SetInternalControlResponse(const InternalControlResponse ctrl_resp);
  bool is_grpc_ready() const;

 private:
  void CreateConnection();
  void UploadConfig();
  void DownloadConfig();
  void Loop();

 private:
  const std::string strategy_name_;
  const std::string server_address_;
  std::vector<std::string> symbols_;
  Handler handler_;
  InternalControlResponse ctrl_resp_;

  volatile bool running_{false};
  std::thread thread_{};

  std::unique_ptr<ConfigReporter_GrpcImpl> grpc_impl_;
};

}  // namespace coin2::strategy::util
